# Default Dockerfile
#
# @link     https://www.hyperf.io
# @document https://hyperf.wiki
# @contact  group@hyperf.io
# @license  https://github.com/hyperf/hyperf/blob/master/LICENSE

FROM hyperf/hyperf:8.0-alpine-v3.12-swoole
LABEL maintainer="Hyperf Developers <group@hyperf.io>" version="1.0" license="MIT" app.name="Hyperf"

##
# ---------- env settings ----------
##
# --build-arg timezone=Asia/Shanghai
ARG timezone

ENV TIMEZONE=${timezone:-"Asia/Shanghai"} \
    APP_ENV=prod \
    SCAN_CACHEABLE=(true)

# update
RUN set -ex \
    # show php version and extensions
    && php -v \
    && php -m \
    && php --ri swoole \
    #  ---------- some config ----------
    && cd /etc/php8 \
    # - config PHP
    && { \
        echo "upload_max_filesize=128M"; \
        echo "post_max_size=128M"; \
        echo "memory_limit=1G"; \
        echo "date.timezone=${TIMEZONE}"; \
    } | tee conf.d/99_overrides.ini \
    # - config timezone
    && ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
    && echo "${TIMEZONE}" > /etc/timezone \
    # ---------- clear works ----------
    && rm -rf /var/cache/apk/* /tmp/* /usr/share/man \
    && echo -e "\033[42;37m Build Completed :).\033[0m\n"

WORKDIR /opt/www

# Composer Cache
# COPY ./composer.* /opt/www/
# RUN composer install --no-dev --no-scripts

COPY . /opt/www
RUN composer install --no-dev -o

# 安装 MySQL 客户端用于执行 SQL 文件
RUN apk add --no-cache mysql-client

# 创建数据库初始化脚本（仅在首次启动时执行）
RUN echo '#!/bin/sh' > /opt/www/init-db.sh && \
    echo 'INIT_FLAG="/opt/www/data/.db_initialized"' >> /opt/www/init-db.sh && \
    echo 'if [ ! -f "$INIT_FLAG" ]; then' >> /opt/www/init-db.sh && \
    echo '  echo "首次启动，开始数据库初始化..."' >> /opt/www/init-db.sh && \
    echo '  echo "等待 MySQL 服务启动..."' >> /opt/www/init-db.sh && \
    echo '  until mysql -h${DB_HOST:-mysql} -u${DB_USERNAME:-techpulse} -p${DB_PASSWORD:-123456} -e "SELECT 1" > /dev/null 2>&1; do' >> /opt/www/init-db.sh && \
    echo "    echo \"等待 MySQL 连接...\"" >> /opt/www/init-db.sh && \
    echo "    sleep 2" >> /opt/www/init-db.sh && \
    echo "  done" >> /opt/www/init-db.sh && \
    echo '  echo "MySQL 已连接，开始执行数据库初始化..."' >> /opt/www/init-db.sh && \
    echo '  mysql -h${DB_HOST:-mysql} -u${DB_USERNAME:-techpulse} -p${DB_PASSWORD:-123456} ${DB_DATABASE:-techpulse} < /opt/www/init.sql' >> /opt/www/init-db.sh && \
    echo '  if [ $? -eq 0 ]; then' >> /opt/www/init-db.sh && \
    echo '    echo "数据库初始化完成！创建标志文件..."' >> /opt/www/init-db.sh && \
    echo '    touch "$INIT_FLAG"' >> /opt/www/init-db.sh && \
    echo '  else' >> /opt/www/init-db.sh && \
    echo '    echo "数据库初始化失败！"' >> /opt/www/init-db.sh && \
    echo '    exit 1' >> /opt/www/init-db.sh && \
    echo '  fi' >> /opt/www/init-db.sh && \
    echo 'else' >> /opt/www/init-db.sh && \
    echo '  echo "数据库已初始化，跳过初始化步骤"' >> /opt/www/init-db.sh && \
    echo 'fi' >> /opt/www/init-db.sh && \
    chmod +x /opt/www/init-db.sh

# 创建启动脚本
RUN echo '#!/bin/sh' > /opt/www/start.sh && \
    echo 'echo "开始启动应用..."' >> /opt/www/start.sh && \
    echo '# 后台执行数据库初始化检查' >> /opt/www/start.sh && \
    echo '/opt/www/init-db.sh &' >> /opt/www/start.sh && \
    echo 'echo "启动 Hyperf 服务..."' >> /opt/www/start.sh && \
    echo 'exec php /opt/www/bin/hyperf.php start' >> /opt/www/start.sh && \
    chmod +x /opt/www/start.sh

EXPOSE 9501

ENTRYPOINT ["/opt/www/start.sh"]
